<Window x:Class="AkzenteIT.DatabaseDocumentationTool.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
        xmlns:Converter="clr-namespace:AkzenteIT.DatabaseDocumentationTool.Converter"
        xmlns:ViewModels="clr-namespace:AkzenteIT.DatabaseDocumentationTool.ViewModels"
        xmlns:Base="clr-namespace:AkzenteIT.DatabaseDocumentationTool.ViewModels.Base" Title="MainWindow" Height="526" Width="878">
    <Window.Resources>
        <Converter:FlattenCategoriesConverter x:Key="FlattenCategoriesConverter" />
        <CollectionViewSource x:Key="DbObjects" Source="{Binding SelectedItem, ElementName=databaseTree, Converter={StaticResource FlattenCategoriesConverter}}" Filter="CollectionViewSourceFilter" >
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Parent" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

        <BitmapImage x:Key="DatabaseIcon" UriSource="pack://application:,,,/Resources/Database.png" />
        <BitmapImage x:Key="RunningIcon" UriSource="pack://application:,,,/Resources/Running2.png" />
        <BitmapImage x:Key="FolderIcon" UriSource="pack://application:,,,/Resources/Folder.png" />
        <BitmapImage x:Key="TableIcon" UriSource="pack://application:,,,/Resources/Table.png" />
        <BitmapImage x:Key="ViewIcon" UriSource="pack://application:,,,/Resources/View.png" />
        <BitmapImage x:Key="ColumnIcon" UriSource="pack://application:,,,/Resources/Column.png" />

        <Style TargetType="{x:Type GroupItem}" x:Key="GroupHeaderStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander x:Name="exp" IsExpanded="True" Background="White" Foreground="Black">
                            <Expander.Header>
                                <TextBlock Text="{Binding Name}"/>
                            </Expander.Header>

                            <ItemsPresenter />
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.Resources>
            <DataTemplate DataType="{x:Type ViewModels:DatabaseViewModel}">
                <Image Source="{StaticResource DatabaseIcon}" Width="16" Height="16" />
            </DataTemplate>
            <DataTemplate DataType="{x:Type ViewModels:ServerViewModel}">
                <Grid>
                    <Image Source="{StaticResource DatabaseIcon}" Width="16" Height="16" />
                    <Image Source="{StaticResource RunningIcon}" Width="16" Height="16" />
                </Grid>
            </DataTemplate>
            <DataTemplate DataType="{x:Type ViewModels:TableViewModel}">
                <Image Source="{StaticResource TableIcon}" Width="16" Height="16" />
            </DataTemplate>
            <DataTemplate DataType="{x:Type ViewModels:ViewViewModel}">
                <Image Source="{StaticResource ViewIcon}" Width="16" Height="16" />
            </DataTemplate>
            <DataTemplate DataType="{x:Type ViewModels:ColumnViewModel}">
                <Image Source="{StaticResource ColumnIcon}" Width="16" Height="16" />
            </DataTemplate>
            <DataTemplate DataType="{x:Type Base:NodeViewModel}">
                <Image Source="{StaticResource FolderIcon}" Width="16" Height="16" />
            </DataTemplate>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="212*" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="530*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="294*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="146*" />
        </Grid.RowDefinitions>
        <TreeView ItemsSource="{Binding Servers}" Name="databaseTree" Grid.RowSpan="4" SelectedItemChanged="DatabaseTreeSelectedItemChanged">
            <!--<TreeView.ItemContainerStyle>

                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="FontWeight" Value="Normal" />
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="FontWeight" Value="Bold" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>-->

            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <ContentPresenter Content="{Binding}" Margin="0,2,4,2">
                        </ContentPresenter>
                        <TextBlock Text="{Binding Mode=OneWay}" Grid.Column="1" VerticalAlignment="Center" />
                    </Grid>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
        <GridSplitter Grid.Column="1" Width="8" VerticalAlignment="Stretch" HorizontalAlignment="Center" ResizeDirection="Columns" Grid.RowSpan="4" />

        <DataGrid CanUserAddRows="False" CanUserDeleteRows="False" ItemsSource="{Binding Source={StaticResource DbObjects}}" AutoGenerateColumns="False" HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch" Grid.Column="2" Margin="0,0,0,9" Grid.Row="1" Grid.ColumnSpan="2">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ContentPresenter Content="{Binding}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Name" Width="1*" Binding="{Binding Name, Mode=OneWay}" IsReadOnly="True" />
                <DataGridTextColumn Header="Description" Width="2*" Binding="{Binding Description}" />
            </DataGrid.Columns>
            <DataGrid.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter />
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
            </DataGrid.GroupStyle>
        </DataGrid>
        <GridSplitter Grid.Column="1" Grid.Row="2" Height="4" VerticalAlignment="Center" HorizontalAlignment="Stretch" ResizeDirection="Rows" Grid.ColumnSpan="2" />

        <Grid Name="ItemDescription" Grid.Column="2" Grid.ColumnSpan="3" DataContext="{Binding SelectedItem.Owner, ElementName=databaseTree}">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Label Content="{Binding}" HorizontalAlignment="Left" Grid.ColumnSpan="2" FontWeight="Bold" />
            <Label Content="Description" Grid.Row="1" Height="28" HorizontalAlignment="Left"  Name="label1" VerticalAlignment="Top" />
            <TextBox Text="{Binding Description, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.Column="1" Height="23" HorizontalAlignment="Stretch" Name="textBox2" VerticalAlignment="Top" />

        </Grid>

        <Grid Grid.Column="2" Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Label Content="Recursion depth" Grid.Column="1" />
            <extToolkit:IntegerUpDown Value="{Binding ScriptingDepth}" Grid.Column="2" />
            <TextBox Name="textBox1"
                     Grid.Column="0" Grid.Row="1"
                     Grid.ColumnSpan="3" 
                     HorizontalScrollBarVisibility="Auto"
                     VerticalScrollBarVisibility="Auto"
                     HorizontalAlignment="Stretch"
                     VerticalAlignment="Stretch"
                     Text="{Binding Script, Mode=OneWay}"
                     AcceptsReturn="True" />
        </Grid>
    </Grid>
</Window>
